package com.atguigu.gulimall.order.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimall.order.to.OrderTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 订单
 *
 * @author tr
 * @email tr@gmail.com
 * @date 2023-04-19 13:26:41
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/member/orders")
    public R memberOrders(){
        List<OrderEntity> orderEntities = orderService.memberOrders();
        List<OrderTo> orderTos =
                orderEntities.stream().map(item->{
                    OrderTo orderTo = new OrderTo();
                    BeanUtils.copyProperties(item,orderTo);
                    return orderTo;
                }).collect(Collectors.toList());
        return R.ok().setData(orderTos);
    }

    /**
     * 根据订单号查询订单状态
     */
    @GetMapping("/orderStatus/{orderSn}")
    public R orderStatus(@PathVariable("orderSn")String orderSn){
        OrderEntity orderEntity = orderService.orderStatus(orderSn);
        OrderTo orderTo = null;
        if(orderEntity!=null){
            orderTo = new OrderTo();
            BeanUtils.copyProperties(orderEntity,orderTo);
        }
        return R.ok().setData(orderTo);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
